//
//  HPBaseNavController.swift
//  
//
//  Created by apple on 16/4/9.
//
//

import UIKit

class HPBaseNavController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
      // 解决手势不能使用的bug
//        self.interactivePopGestureRecognizer?.delegate = nil
        interactivePopGestureRecognizer?.delegate = self

    }
    
    //MARK: 实现代理方法, 解决了控制器跳转时卡顿的情况, 如果不是首页不响应手势
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        print("手势识别")
        return childViewControllers.count != 1
        
    }
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        // 判断是否为第一个控制器
        if childViewControllers.count > 0 {
            var backTitle = "返回"
            if childViewControllers.count == 1 {
//                backTitle = childViewControllers.first?.title ?? ""
                backTitle = childViewControllers.first?.title ?? ""
//                backTitle = "首页"
                
            }
//            需要用参数viewController 来调用
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: "backPrevious")
            viewController.hidesBottomBarWhenPushed = true
        }
        // 需要在外部调用,
        //调用super 之前隐藏tabBar  的状态栏
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func backPrevious() {
        
        popViewControllerAnimated(true)
    }
    
}
